package main.pmc;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;
import utils.httpclient.HttpClient;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/3/12 13:20
 */
public class DownPmcidHtmlFromOtherDataFrom {
    // 用于合并老板抓取的html 合并到一起

    @SneakyThrows
    public static void main(String[] args) {
//        DSFactory.setCurrentDSFactory(new DruidDSFactory());
//        DataSource ds = DSFactory.get("taiyiserver");
        Db useTaiyi = Db.use();
        Db localMe = Db.use(DSFactory.get("local_data_com"));
        String saveTable = "pmcdownziphtml";
        String from = "spider_pubmed_vip_extend_0114_pmc_save_1";
        String allDate = "select max(id) as id  from " + from;
        Entity query = localMe.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("id"));
        Integer count = query.getInt("id");
        int breakSize = 10000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("即将开始请提前关闭如果想的话");
        SmallTool.SleepMillis(5000L);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("当前页码+++++" + i);
            SmallTool.printMess("开始id++" + start + "+++结束id++++" + end);
            String paramSql = "select pmid,pmcid,html from " + from + " where   id between  " + start + " and " + end;
            List<Entity> querys = localMe.query(paramSql);
            querys.parallelStream().forEach(entity -> {
                String pmcid = entity.getStr("pmcid");
                Integer pmid = entity.getInt("pmid");
                byte[] htmls = entity.getBytes("html");
                if (StringUtil.isNotNull(pmcid) && StringUtil.isNotNull(htmls) && StringUtil.isNotNull(pmcid)) {
                    // 查询 pmcdownziphtml 表中是否存在该 pmid
                    String checkSql = "select * from " + saveTable + " where pmid = ?";
                    List<Entity> checkResult = null;
                    try {
                        checkResult = useTaiyi.query(checkSql, pmid);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    }
                    if (checkResult.isEmpty()) {
                        // 不存在则插入
                        Entity newEntity = Entity.create(saveTable)
                                .set("pmid", pmid)
                                .set("pmcid", pmcid)
                                .set("html", htmls);
                        try {
                            useTaiyi.insert(newEntity);
                        } catch (SQLException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        // 存在则判断 html 字段是否为 null
                        Entity existingEntity = checkResult.get(0);
                        byte[] existingHtml = existingEntity.getBytes("html");
                        if (StringUtil.isNull(existingHtml)) {
                            // html 字段为 null 则更新
                            existingEntity.set("html", htmls);
                            existingEntity.setTableName(saveTable);
                            try {
                                useTaiyi.insertOrUpdate(existingEntity, "pmid");
                            } catch (SQLException e) {
                                throw new RuntimeException(e);
                            }
                        } else {
//                            SmallTool.printMess("已经存在该数据了");
                        }
                    }
                }
            });

        }


    }


}
